package com.xinke.edu.mypetadoption.Activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xinke.edu.mypetadoption.Activity.adapter.CheckoutAdapter;
import com.xinke.edu.mypetadoption.databinding.ActivityCheckoutBinding;
import com.xinke.edu.mypetadoption.util.ShoppingCartManager;

import java.text.DecimalFormat;

/**
 * 结算界面
 */
public class CheckoutActivity extends AppCompatActivity {
    ActivityCheckoutBinding binding;
    private CheckoutAdapter checkoutAdapter;
    private static final DecimalFormat df = new DecimalFormat("0.00");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCheckoutBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());

        // 初始化RecyclerView
        initRecyclerView();
        
        // 加载结算数据
        loadCheckoutData();

        // 设置地址和联系电话
        setAddressAndPhone();

        // 点击左上角返回按钮
        binding.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 点击提交订单按钮
        binding.submitOrderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitOrder();
            }
        });
    }

    // 初始化RecyclerView
    private void initRecyclerView() {
        checkoutAdapter = new CheckoutAdapter();
        binding.checkoutItemsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.checkoutItemsRecyclerView.setAdapter(checkoutAdapter);
    }

    // 加载结算数据
    private void loadCheckoutData() {
        // 从购物车管理器获取数据
        checkoutAdapter.setCartItems(ShoppingCartManager.getInstance().getCartItems());
        
        // 计算并显示总金额和总数量
        calculateAndShowTotal();
    }

    // 计算并显示总金额和总数量
    private void calculateAndShowTotal() {
        double totalPrice = ShoppingCartManager.getInstance().calculateTotalPrice();
        int totalQuantity = ShoppingCartManager.getInstance().getItemCount();
        
        binding.totalPriceTextView.setText("¥" + df.format(totalPrice));
        binding.totalQuantityTextView.setText("共" + totalQuantity + "件商品");
    }

    // 设置地址和联系电话（这里使用模拟数据，实际应用中应该从用户信息或本地存储获取）
    private void setAddressAndPhone() {
        // 模拟地址和联系电话
        String address = "北京市朝阳区科技园123号宠物之家";
        String phone = "13812345678";
        
        binding.addressTextView.setText(address);
        binding.phoneTextView.setText(phone);
    }

    // 提交订单
    private void submitOrder() {
        // 这里可以实现提交订单的逻辑，如调用API等
        Toast.makeText(this, "订单提交成功！", Toast.LENGTH_SHORT).show();
        
        // 提交成功后可以清空购物车并返回首页或订单列表页
        ShoppingCartManager.getInstance().clear();
        finish();
    }
}